﻿Imports System.Windows.Forms
Imports System.Collections


Public Class Login

    Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitQuanLyBanHang()
        txtUsername.Text = "Admin"
        txtPassword.Text = "tandongtien"
        swbDepartment.Value = True
    End Sub

    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        ValidateData()

        Dim userbus As New UserBUS()
        Dim dt As DataTable
        Dim bolAuthentication As Boolean
        Dim bolChangpw As Boolean

        dt = userbus.GetUserByUserName(txtUsername.Text).Tables(0)
        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                If Trim(txtPassword.Text) = mdQuanLyBanHang.StringDecode(.Item("Password")) Then
                    If .Item("StatusFlag") = True Then
                        bolAuthentication = True

                        If .Item("PrivilegeName") = "Administrator" Then
                            objUser.Administrator = True
                        Else
                            objUser.Administrator = False
                        End If
                        objUser.UserFullName = .Item("FullName")
                        objUser.UserAccountID = .Item("UserAccountID")
                        objUser.UserName = Trim(txtUsername.Text)
                        objUser.Privilege = .Item("PrivilegeID")
                        objUser.NonFlag = .Item("NoneFlag")
                        objUser.ReadonlyFlag = .Item("ReadOnlyFlag")
                        objUser.FullFlag = .Item("FullFlag")
                        objUser.CashBookFlag = .Item("CashBookFlag")

                        'Me.Close()
                        If .Item("PasswordChgAtLogonFlag") = True Then
                            bolChangpw = True
                        End If

                        mdRegistry.SetKeyUserName_Department(txtUsername.Text, swbDepartment.Value)

                        If swbDepartment.Value = True Then
                            eDepartment = mdQuanLyBanHang.Department.Sales
                        Else
                            eDepartment = mdQuanLyBanHang.Department.Financial
                        End If
                        Dim dateExport As Date

                        Select Case eDateTimeFormatType
                            Case mdQuanLyBanHang.DateTimeFormatType.DMY
                                dateExport = Format(DateAdd(DateInterval.Year, -1, Now), "dd/MM/yyyy")
                            Case mdQuanLyBanHang.DateTimeFormatType.MDY
                                dateExport = Format(DateAdd(DateInterval.Year, -1, Now), "MM/dd/yyyy")
                            Case mdQuanLyBanHang.DateTimeFormatType.YDM
                                dateExport = Format(DateAdd(DateInterval.Year, -1, Now), "yyyy/dd/MM")
                            Case mdQuanLyBanHang.DateTimeFormatType.YMD
                                dateExport = Format(DateAdd(DateInterval.Year, -1, Now), "yyyy/MM/dd")
                        End Select

                        'With myCommand
                        '    .Connection = cnnDB
                        '    .CommandType = CommandType.StoredProcedure
                        '    .CommandText = "spTDT_ExportItems_Delete_Forever"
                        '    .Parameters.Clear()
                        '    .Parameters.Add("@ExportDate", OleDbType.Date).Value = dateExport
                        '    .ExecuteNonQuery()
                        'End With

                        Dim exportbus As New ExportItemsBUS()
                        exportbus.ExportItems_Delete_Forever(dateExport)

                        '    With myCommand
                        '        .Parameters.Clear()
                        '        .CommandType = CommandType.StoredProcedure
                        '        .CommandText = "spTDT_Items_RefreshQuantity"
                        '        .ExecuteNonQuery()
                        '    End With

                        Dim itemBUS As New ItemsBUS()
                        itemBUS.Items_RefreshQuantity()

                    ElseIf .Item("StatusFlag") = False Then
                        bolAuthentication = False
                        MsgBox("Tài khoản của bạn đã bị khóa. Xin vui lòng thông báo với Người quản trị để bạn có thể đăng nhập lại", MsgBoxStyle.Critical, "Tài khoản")
                    End If
                Else
                    bolAuthentication = False
                    MsgBox("Tên truy cập hoặc mật khẩu không hợp lệ", MsgBoxStyle.Critical, "Lỗi đăng nhập")
                End If
            End With
        Else
            bolAuthentication = False
            MsgBox("Tên truy cập hoặc mật khẩu không hợp lệ", MsgBoxStyle.Critical, "Lỗi đăng nhập")
        End If
    End Sub

    Private Function ValidateData() As Boolean
        ValidateData = True
        Try
            If CheckExistComputerName(objConnectionDAO.connectdto.ServerName) = False Then
                ValidateData = False
                MsgBox("Máy chủ không tồn tại. Xin vui lòng kiểm tra lại.", MsgBoxStyle.Critical, "Lỗi kết nối")
                Exit Function
            End If
            If txtUsername.Text = "" Then
                'ErrorProvider1.SetError(txtUsername, "Làm ơn nhập tên đăng nhập")
                txtUsername.Focus()
                txtUsername.FocusHighlightColor = Color.Orange

                ValidateData = False
            Else
                'ErrorProvider1.SetError(txtUsername, "")
            End If
            If txtPassword.Text = "" Then
                'ErrorProvider1.SetError(txtPassword, "Làm ơn nhập mật khẩu")
                txtPassword.Focus()
                txtPassword.FocusHighlightColor = Color.Orange

                ValidateData = False
            Else
                'ErrorProvider1.SetError(txtPassword, "")
            End If

        Catch ex As Exception
            objErrorLog.SaveError(ex.Message, Me.Text, "ValidateData", txtUsername.Text)
            'SendError(ex)
            ValidateData = False
        End Try

    End Function

    Private Function CheckExistComputerName(ByVal strServerOrIPAddress As String) As Boolean
        CheckExistComputerName = True
        Dim strResult As String
        Try
            strServerOrIPAddress = Trim(strServerOrIPAddress.Replace("\sqlexpress", ""))
            If InStr(strServerOrIPAddress, ".", CompareMethod.Text) = 0 Then
                'strResult = System.Net.Dns.GetHostByName(strServerOrIPAddress).HostName
                strResult = System.Net.Dns.GetHostEntry(strServerOrIPAddress).HostName
            Else
                'strResult = System.Net.Dns.GetHostByAddress(strServerOrIPAddress).HostName
                strResult = System.Net.Dns.GetHostEntry(strServerOrIPAddress).HostName
            End If
            If strResult = "" Then
                CheckExistComputerName = False
            End If
        Catch ex As Exception
            CheckExistComputerName = False
        End Try
    End Function

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click
        Me.ParentForm.Close()
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        Dim exbus As New ExportItemsBUS()
        DataGridViewX1.DataSource = exbus.ExportItems_List()
    End Sub

    Private Sub ButtonX3_Click(sender As Object, e As EventArgs) Handles ButtonX3.Click
        Dim itdbus As New ImportItemDetailsBUS()
        LabelX1.Text = itdbus.ImportItemDetails_PermitCheck(1, 1)
    End Sub
End Class
